package com.ehouse.adapters;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.ResourceCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ehouse.R;
import com.ehouse.database.LightScheduleTable;

public class ScheduleLightItemAdapter extends ResourceCursorAdapter{
	private int layout;
	
	public ScheduleLightItemAdapter (Context context, int layout, Cursor c, int flags){
		super(context, layout, c, flags);
		this.layout = layout;
	}

	    @Override
	    public View newView(Context context, Cursor cur, ViewGroup parent) {
	        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        return li.inflate(layout, parent, false);
	    }

	    @Override
	    public void bindView(View view, Context context, Cursor cursor) {
	    	String time = cursor.getString(cursor.getColumnIndex(LightScheduleTable.COLUMN_TIME));
	    	String detail = cursor.getString(cursor.getColumnIndex(LightScheduleTable.COLUMN_DETAIL));
	    	boolean state = (cursor.getInt(cursor.getColumnIndex(LightScheduleTable.COLUMN_STATE)) == 1);
	    	int stateResource = R.string.switch_off;
	    	
	    	if(state)
	    		stateResource = R.string.switch_on;
	    	view.setTag(LightScheduleTable.COLUMN_OBJ_ID);
	    	
	    	TextView scheduleLabel = (TextView)view.findViewById(R.id.scheduleLabel);
	    	TextView detailLabel = (TextView)view.findViewById(R.id.detailLabel);
	    	scheduleLabel.setText(context.getResources().getString(R.string.at) + " " + time + " " + context.getResources().getString(R.string.lights_label) + " " + context.getResources().getString(stateResource));
	    	detailLabel.setText(detail);
	    }
	    
	    

}
